import 'package:dio/dio.dart';

import '../http/http.dart';

class HttpHelper {
  const HttpHelper._();

  static void init({
    required String baseUrl,
    int connectTimeout = 2000,
    int receiveTimeout = 2000,
    List<Interceptor>? interceptors,
  }) {
    Http().init(
      baseUrl: baseUrl,
      connectTimeout: connectTimeout,
      receiveTimeout: receiveTimeout,
      interceptors: interceptors,
    );
  }

  /// 设置默认options
  static void setOptions(Options defaultOptions) {
    Http().setOptions(defaultOptions);
  }

  /// 添加拦截器
  static void addInterceptors(List<Interceptor> interceptors) {
    Http().addInterceptors(interceptors);
  }

  static void cancelRequests({required CancelToken token}) {
    Http().cancelRequests(token: token);
  }

  static Future get(
    String path, {
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    bool refresh = false,
    bool noCache = true, //!CACHE_ENABLE,
    String? cacheKey,
    bool cacheDisk = false,
    bool showLoading = true,
  }) async {
    var response =  await Http().get(
      path,
      params: params,
      options: options,
      cancelToken: cancelToken,
      refresh: refresh,
      noCache: noCache,
      cacheKey: cacheKey,
      showLoading: showLoading,
    );
    return response['code'] == 200 ? response['data'] : null;
  }

  static Future post(
    String path, {
    data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    bool showLoading = true,
  }) async {
    var response = await Http().post(
      path,
      data: data,
      params: params,
      options: options,
      cancelToken: cancelToken,
      showLoading: showLoading,
    );
    return response['code'] == 200 ? response['data'] : null;
  }

  static Future put(
    String path, {
    data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    bool showLoading = true,
  }) async {
    var response =  await Http().put(
      path,
      data: data,
      params: params,
      options: options,
      cancelToken: cancelToken,
      showLoading: showLoading,
    );
    return response['code'] == 200 ? response['data'] : null;
  }

  static Future patch(
    String path, {
    data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    bool showLoading = true,
  }) async {
    var response =  await Http().patch(
      path,
      data: data,
      params: params,
      options: options,
      cancelToken: cancelToken,
      showLoading: showLoading,
    );
    return response['code'] == 200 ? response['data'] : null;
  }

  static Future delete(
    String path, {
    data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    bool showLoading = true,
  }) async {
    var response =  await Http().delete(
      path,
      data: data,
      params: params,
      options: options,
      cancelToken: cancelToken,
      showLoading: showLoading,
    );
    return response['code'] == 200 ? response['data'] : null;
  }
}
